package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }
    //分页查询
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false)String key){
        return  ResponseEntity.ok(this.brandService.queryByPage(page,rows,key));
    }
    //新增品牌
    @PostMapping
    public ResponseEntity<Void> addBrand( BrandDTO brandDTO,
                                          @RequestParam("categoryIds") List<Long> cids){

        brandService.addBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //修改品牌
    @PutMapping
    public ResponseEntity<Void>  updateBrand(BrandDTO brandDTO,
                                             @RequestParam("categoryIds") List<Long> cids
    ){
        brandService.updateBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //根据品牌ID集合查询品牌分类
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){

        return ResponseEntity.ok(this.brandService.queryBrandByIds(ids));
    }
    //根据分类ID查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCid(@RequestParam("id") Long cid){
        return ResponseEntity.ok(this.brandService.queryBrandByCid(cid));
    }
    //根据ID查询品牌
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }
    @Autowired
     private CategoryBrandService categoryBrandService;

    //根据ID删除品牌
    @Delete("{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id") Long id){
        // 删除品牌表
        brandService.deleteById(id);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}